package com.wd.tech.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;


import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wd.tech.R;
import com.wd.tech.adapter.News.NewShowAdapter;
import com.wd.tech.base.BaseFragment;
import com.wd.tech.base.BasePresenter;
import com.wd.tech.bean.News_Show_Bean;
import com.wd.tech.mvp.IPresenterImpl;
import com.wd.tech.url.BaseUrl;
import com.wd.tech.utils.NetUtil;
import com.wd.tech.view.activity.NewsMoreActivity;
import com.wd.tech.view.activity.SearchforActivity;

import java.util.HashMap;
import java.util.Map;

public class ZiXunFragment extends BaseFragment {

    private ImageView news;
    private RecyclerView shownew;
    private ImageView SearchFor;
    private boolean wang;
    private NewShowAdapter newShowAdapter;


    @Override
    protected void initData() {

        // TODO: 2020/7/14 判断网络
        wang = NetUtil.getInstance().HasNet();
        //展示资讯首页、
        Map<String ,Object> show = new HashMap<>();
        show.put("plateId",1);
        show.put("page",1);
        show.put("count",15);
        presenter.PresenterGetNewsShow(BaseUrl.News_Sh,show,News_Show_Bean.class);
        shownew.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    protected BasePresenter initPresenter() {
        return new IPresenterImpl();
    }

    @Override
    protected void initView(View view) {

        news = view.findViewById(R.id.News_More);
        shownew = view.findViewById(R.id.show_news);
        SearchFor = view.findViewById(R.id.Search_for);

        //搜索
        SearchFor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchforActivity.class);
                startActivity(intent);
            }
        });
        //频道
        news.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), NewsMoreActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected int layoutId() {
        return R.layout.zixun_fragment;
    }

    @Override
    public void success(Object o) {

        if (wang){
            if (o instanceof News_Show_Bean){
                newShowAdapter = new NewShowAdapter(getActivity(), ((News_Show_Bean) o).getResult());
                shownew.setAdapter(newShowAdapter);
                newShowAdapter.notifyDataSetChanged();
            }
        }

    }

    @Override
    public void Error(Throwable throwable) {

    }
}
